//
//  TitleView.m
//
//  Created by dllo on 15/4/16.
//

#import "TitleView.h"
#import "DEFINE.h"

@implementation TitleView

- (void)dealloc
{    
    [_titleArr release];
    [_titleView release];
    [_buttonArr release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // 按钮数组
        self.buttonArr = [[NSMutableArray alloc] init];
        // 按钮标题
        self.titleArr = [NSMutableArray arrayWithObjects:@"推荐",@"乐库",@"搜索", @"电台", nil];
        for (int i = 0; i < 4; i++) {
            UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
            titleButton.frame = CGRectMake((SCREEN_WIDTH/5)*i+10, 0, 50*ADJUSTWIDTH, 44);
            [titleButton setTitle:_titleArr[i] forState:UIControlStateNormal];
            [titleButton setTitleColor:[UIColor whiteColor] forState:0];
            [titleButton setTitleColor:COLOR(181, 50, 124, 1.0)
                              forState:UIControlStateSelected];
            titleButton.titleLabel.font = [UIFont systemFontOfSize:17];
            [titleButton addTarget:self action:@selector(titleClick:)
                  forControlEvents:UIControlEventTouchUpInside];
            titleButton.tag = 10001 + i;            
            [self addSubview:titleButton];
            [self.buttonArr addObject:titleButton];
        }
        UIButton *sender_one = [self.buttonArr objectAtIndex:0];
        sender_one.selected = YES;
        self.titleView = [[UIView alloc]initWithFrame:CGRectMake(10, 41, 50*ADJUSTWIDTH, 4)];
        self.titleView.backgroundColor = COLOR(181, 50, 124, 1.0);
        [self addSubview:self.titleView];
        [_titleView release];
        [_buttonArr release];
        
    }
    return self;
}


- (void)titleClick:(UIButton *)sender
{
    // 判断点击按钮 遍历button 数组指针对应的button装态是一致的
    // button 的单选状态
    NSInteger page = sender.tag - 10001;
    for (UIButton *button in self.buttonArr) {
        if (sender == button) {
            button.selected = YES;
            [self.myChangeDelegate changeOffSet:button.tag];
            
            [UIView animateWithDuration:0.3 animations:^{
                
                self.titleView.frame = CGRectMake(SCREEN_WIDTH / 5 * page+10 , 41, SCREEN_WIDTH / 7, 3);
                
                
            } completion:^(BOOL finished) {
                
            }];
            
        }
        else{
            button.selected = NO;
        }
    }
}






@end
